package com.lions.client.view.gui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;

/**
 * 
 * @author Eric
 * 
 */
public class LoginPanel extends DockPanel {

	public LoginPanel(LoginHandler h) {
		super();
		handler = h;
		
		passBox.setSize("100%", "100%");
		userBox.setSize("100%", "100%");
		
		loginPanel.setStyleName("login");
		loginPanel.setWidget(0, 0, userLabel);
		loginPanel.setWidget(0, 1, userBox);
		loginPanel.setWidget(0, 2, userStatus);
		
		loginPanel.setWidget(1, 0, passLabel);
		loginPanel.setWidget(1, 1, passBox);
		loginPanel.setWidget(1, 2, passStatus);
		
		loginPanel.setWidget(2, 0, submit);
		submit.setStyleName("buttonstyle");
		userStatus.setVisible(false);
		passStatus.setVisible(false);
		
		setHorizontalAlignment(DockPanel.ALIGN_CENTER);
		setStyleName("dockpanel");
		add(loginPanel, DockPanel.CENTER);
		this.setVisible(true);
		createHandlers();
		
	}
	
	public boolean getStatus(){
		return status;
	}
	
	private void setStatus(boolean s){
		status = s;
	}
	
	private void createHandlers(){
		submit.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent e){
				if ((userBox.getText().toUpperCase() == null)
					&& (passBox.getText().toUpperCase() == null)){
						userStatus.setText("Username invalid");
						passStatus.setText("Password invalid");
						userStatus.setVisible(true);
						passStatus.setVisible(true);
				}				
				else if (userBox.getText().toUpperCase().equals("USER1")){
						setStatus(true);
						handler.loginSuccess();
				}
				else if (userBox.getText().toUpperCase().equals("ERIC")){
					if	(passBox.getText().toUpperCase().equals("WELLMAN")){
						setStatus(true);
						setVisible(false);
					}
					else{
						passStatus.setText("Password invalid");
						passStatus.setVisible(true);
						userStatus.setVisible(false);
					}
				}
				else if (userBox.getText().toUpperCase().equals("BRAD")){
					if (passBox.getText().toUpperCase().equals("CUTSHALL")){
						setStatus(true);
						handler.loginSuccess();
						//System.out.println("LoginSuccess");
					}
					else{
						passStatus.setText("Password invalid");
						passStatus.setVisible(true);
						userStatus.setVisible(false);
					}
				}	
				else if (userBox.getText().toUpperCase().equals("KYLE")){
					if (passBox.getText().toUpperCase().equals("FINK")){
						setStatus(true);
						handler.loginSuccess();
					}
					else{
						passStatus.setText("Password invalid");
						passStatus.setVisible(true);
						userStatus.setVisible(false);
					}
				}	
				else if (userBox.getText().toUpperCase().equals("TONY")){
					if (passBox.getText().toUpperCase().equals("MENGELKOCH")){
						setStatus(true);
						handler.loginSuccess();
					}
					else{
						passStatus.setText("Password invalid");
						passStatus.setVisible(true);
						userStatus.setVisible(false);
					}
				}	
				else{
					userStatus.setText("Username invalid");
					userStatus.setVisible(true);
					passStatus.setVisible(false);
				}
			}
		});
	}
	
private LoginHandler handler;	
private FlexTable loginPanel = new FlexTable();
private final Button submit = new Button("Submit");
private TextBox userBox = new TextBox();
private PasswordTextBox passBox = new PasswordTextBox();
private Label userLabel = new Label("Username:");
private Label passLabel = new Label("Password:");
private Label userStatus= new Label("");
private Label passStatus = new Label("");
private boolean status = false;
}